package com.web.controller;

import com.web.bean.讲授;
import com.web.dao.讲授Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet("/insertTeach")
public class TeachInsertController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String 教师编号 = req.getParameter("教师编号");
        String 课程代号 = req.getParameter("课程代号");
        String 任课时间 = req.getParameter("任课时间");

        讲授 teach = new 讲授(教师编号, 课程代号,任课时间);
        讲授Dao dao = new 讲授Dao();
        boolean isok = false;
        try {
            isok = dao.insertTeach(teach);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (isok) {
            req.getRequestDispatcher("/getAllTeach").forward(req, resp);
        } else {

            req.setAttribute("message", "添加操作失败，请重新操作");
            req.setAttribute("teach", teach);
            req.getRequestDispatcher("/TeachInsert.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}